<?php

/**
 * 修复批次记录
 * @author HelloWorld
 * 2023-8-8
 * UTF-8
 */
class RepairbatchController extends Controller_Base
{
    //修复批次
    public function IndexAction()
    {
        $request = $this->getRequest();
        $page = intval($request->getQuery('page', 1));
        $limit = intval($request->getQuery('limit', 10));
        $batchNo = $request->getQuery('batchNo', '');
        $batchName = $request->getQuery('batchName', '');
        $repairStatus = $request->getQuery('repairStatus', 0);
        $creatId = $request->getQuery('creatId', 0);
        $submitId = $request->getQuery('submitId', 0);
        $repairType = $request->getQuery('repairType', 0);
        $where = $this->getWhere('creatId');
        if($batchNo){
            array_push($where,"batchNo = '{$batchNo}'");
        }
        if($creatId){
            array_push($where,"creatId = '{$creatId}'");
        }
        if($submitId){
            array_push($where,"submitId = '{$submitId}'");
        }
        if($repairStatus){
            array_push($where,"repairStatus = '{$repairStatus}'");
        }
        if($repairType){
            array_push($where,"repairType = '{$repairType}'");
        }
        if($batchName){
            array_push($where,"batchName like '%{$batchName}%'");
        }
        $add_begin_end = trim($this->getRequest()->getQuery('add_begin_end', ''));
        if ($add_begin_end){
            $date_tmp =  explode(" 到 ",$add_begin_end);
            $begin_date = $date_tmp[0];
            $end_date = $date_tmp[1];
            if (!empty($begin_date) || !empty($end_date)) {
                $begin_date = $begin_date ? $begin_date . " 00:00:00" : "";
                $end_date = $end_date ? $end_date . " 23:59:59" : "";
            }
            if ($end_date) {
                $end_date = strtotime($end_date);
                array_push($where, "creatTime <= {$end_date}");

            }
            if ($begin_date) {
                $begin_date = strtotime($begin_date);
                array_push($where, "creatTime >= {$begin_date}");
            }
        }
        $submit_add_begin_end = trim($this->getRequest()->getQuery('submit_add_begin_end', ''));
        if ($submit_add_begin_end){
            $date_tmp =  explode(" 到 ",$submit_add_begin_end);
            $submit_begin_date = $date_tmp[0];
            $submit_end_date = $date_tmp[1];
            if (!empty($submit_begin_date) || !empty($submit_end_date)) {
                $submit_begin_date = $submit_begin_date ? $submit_begin_date . " 00:00:00" : "";
                $submit_end_date = $submit_end_date ? $submit_end_date . " 23:59:59" : "";
            }
            if ($submit_end_date) {
                $submit_end_date = strtotime($submit_end_date);
                array_push($where, "submitTime <= {$submit_end_date}");

            }
            if ($submit_begin_date) {
                $submit_begin_date = strtotime($submit_begin_date);
                array_push($where, "submitTime >= {$submit_begin_date}");
            }
        }
        $page = $page ? $page : 1;
        $start = ($page - 1) * $limit;
        $query = [
            'batchNo' => $batchNo,
            'creatId' => $creatId,
            'submitId' => $submitId,
            'repairStatus' => $repairStatus,
            'repairType' => $repairType,
            'add_begin_end' => $add_begin_end,
            'submit_add_begin_end' => $submit_add_begin_end,
            'limit' => $limit,
            'batchName' => $batchName,
        ];
        $seacher = array(
            'where' => $where,
            'limit' => $limit,
            'start' => $start,
            'order' => 'id DESC'
        );
        $batch = Module_Repair_RepairBatch::getInstance()->getAllBySearch(array("*"), $seacher, true);
        //更新记录
        foreach ($batch as &$val){
            $val['rate'] = $val['successNum'] > 0 ? sprintf("%.4f", $val['successNum']/$val['repairNum']) * 100 : 0;

            if($val['repairStatus'] == 1){
                $record = Module_Repair_RepairRecord::getInstance()->getAllBySearch(array("count(*) as num"), array('where' => ["batchId = {$val['id']}"]));
                if($record[0]['num'] >= $val['repairNum']){
                    Module_Repair_RepairBatch::getInstance()->singleUpdate(['id'=>$val['id'],'repairStatus'=>2]);
                    $val['repairStatus'] = 2;
                }
            }
        }

        $total = Module_Repair_RepairBatch::getInstance()->getAllBySearch(array("count(*) as num"), array('where' => $where), true);
        $total = $total[0]['num'];
        $totalPage = ceil($total / $limit);
        //赋值调解员
        $this ->getMediator();
        SmartyAdapter::assign('arrList', $batch);
        SmartyAdapter::assign('totalPage', $totalPage);
        SmartyAdapter::assign('totalnum', $total);
        SmartyAdapter::assign('pagesize', $limit);
        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('query', $query);
        SmartyAdapter::assign('repairStatus', Module_Repair_RepairBatch::$repairStatusArr);
        SmartyAdapter::assign('page_query', http_build_query($query));

    }
    //批次详情
    public function batchDetailAction()
    {
        $request = $this->getRequest();
        $batchId = $request->getQuery('batchId', '');
        $page = intval($request->getQuery('page', 1));
        $limit = intval($request->getQuery('limit', 10));
        if(empty($batchId)){
            $this->displayError('缺少必要参数');
        }
        $repairStatus = $request->getQuery('repairStatus', 0);
        $idCard = $request->getQuery('idCard', '');
        $repairName = $request->getQuery('repairName', '');
        $where = $this->getWhere('creatId');
        array_push($where,"batchId = {$batchId}");
        if($repairStatus){
            array_push($where,"repairStatus = '{$repairStatus}'");
        }
        if($idCard){
            array_push($where,"idCard = '{$idCard}'");
        }
        if($repairName){
            array_push($where,"repairName = '{$repairName}'");
        }
        $page = $page ? $page : 1;
        $start = ($page - 1) * $limit;
        $query = [
            'repairStatus' => $repairStatus,
            'idCard' => $idCard,
            'repairName' => $idCard,
            'batchId' => $batchId,
            'limit' => $limit,
        ];
        $seacher = array(
            'where' => $where,
            'limit' => $limit,
            'start' => $start,
            'order' => 'id DESC'
        );
        $record = Module_Repair_RepairRecord::getInstance()->getAllBySearch(array("*"), $seacher, true);
        $total = Module_Repair_RepairRecord::getInstance()->getAllBySearch(array("count(*) as num"), array('where' => $where), true);
        $total = $total[0]['num'];
        $totalPage = ceil($total / $limit);
        //赋值调解员
        $this ->getMediator();
        SmartyAdapter::assign('arrList', $record);
        SmartyAdapter::assign('totalPage', $totalPage);
        SmartyAdapter::assign('totalnum', $total);
        SmartyAdapter::assign('pagesize', $limit);
        SmartyAdapter::assign('curPageNum', $page);
        SmartyAdapter::assign('query', $query);
        SmartyAdapter::assign('repairStatus', Module_Repair_RepairRecord::$repairStatusArr);
        SmartyAdapter::assign('page_query', http_build_query($query));
    }
    //批次提交
    public function submitISPAction(){
        $batchId = $this->_request->getPost('id', 0);
        if (!$batchId) {
            AjaxTool::returnJsonData(4005);
        }
        $batch = Module_Repair_RepairBatch::getInstance()->getOne(['*'],["id={$batchId}",'repairStatus=2']);
        if(empty($batch)){
            AjaxTool::returnJsonData(4003,'该批次已提交或批次不存在');

        }
        $res = Module_Repair_RepairBatch::getInstance()->singleUpdate([
            'id'=>$batchId,
            'repairStatus'=>3,
            'desc'=>'',
            'submitTime'=>time(),
            'submitId'=>$this->admin['id'],
            'submitName'=>$this->admin['name'],
        ]);
        if(!$res) {
            AjaxTool::returnJsonData(5000, '状态修改错误，批次提交失败');

        }
        $post_data = [
            'type' => 'Module_Task_Repair',
            'method' => 'submitISP',
            'data' => ['batchId'=>$batchId,'repairType'=>$batch['repairType']]
        ];
        @Module_Queue_Base::addQueue($post_data,1,2);

        AjaxTool::returnJsonData(200,'提交成功');


    }
    //删除批次
    public function deleteRecordAction()
    {
        $request = $this->getRequest();
        $recordId = $request->getPost('recordId', 0);
        if (empty($recordId)) {
            AjaxTool::returnJsonData(400, "缺少参数");
        }
        //判断是否被提交了,提交了 就不能删除
        $entryRecord = Module_Repair_RepairBatch::getInstance()->getItemById($recordId);
        if (empty($entryRecord)) {
            AjaxTool::returnJsonData(400, "批次不存在");
        }

        if ($entryRecord['repairStatus'] > 2) {
            AjaxTool::returnJsonData(400, "已提交的修复不能删除");
        }

        Module_Repair_RepairBatch::getInstance()->beginTransaction();
        $res = Module_Repair_RepairBatch::getInstance()->deleteItemById($recordId);
        if (!$res) {
            Module_Repair_RepairBatch::getInstance()->rollback();
            AjaxTool::returnJsonData(400, "删除失败");
        }
        $res = Module_Repair_RepairRecord::getInstance()->deleteItemById($recordId, 'batchId');
        if (!$res && $res !== 0) {
            Module_Repair_RepairBatch::getInstance()->rollback();
            AjaxTool::returnJsonData(400, "修复记录删除失败");
        }
        Module_Repair_RepairBatch::getInstance()->commit();
        AjaxTool::returnJsonData(200, "操作成功");
    }
}
